package com.fh.controller.airsys.registeronlinetran;

import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.fh.Enum.EnFinalValue;
import com.fh.entity.system.User;
import com.fh.service.airsys.airlineemployee.AirlineEmployeeService;
import com.fh.util.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fh.controller.base.BaseController;
import com.fh.entity.Page;
import com.fh.service.airsys.registeronlinetran.RegisteronLineTranService;

/** 
 * 类名称：RegisteronLineTranController
 * 创建人：FH 
 * 创建时间：2017-11-14
 */
@Controller
@RequestMapping(value="/registeronlinetran")
public class RegisteronLineTranController extends BaseController {
	
	String menuUrl = "registeronlinetran/list.do"; //菜单地址(权限用)
	@Resource(name="registeronlinetranService")
	private RegisteronLineTranService registeronlinetranService;
	@Resource(name="airlineemployeeService")
	private AirlineEmployeeService airlineemployeeService;
	
	/**
	 * 新增
	 */
	@RequestMapping(value="/save")
	public ModelAndView save() throws Exception{
		logBefore(logger, "新增RegisteronLineTran");
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "add")){return null;} //校验权限
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		pd.put("REGISTERONLINETRAN_ID", this.get32UUID());	//主键
		pd.put("RL_CODE",this.get32UUID());
		Session session = getSession();
		User user = (User) session.getAttribute(Const.SESSION_USER);
		if(user == null){
			throw new Exception("请重新登录");
		}
		else{
			pd.put("AL_CODE",user.getAIRLINEINFO_ID());
			pd.put("RI_CODE",user.getAIRLINERUMMERY_ID());
			pd.put("RL_SUBMIT_USERNAME",user.getUSER_ID());
		}
		registeronlinetranService.save(pd);
		mv.addObject("msg","success");
		mv.setViewName("save_result");
		return mv;
	}
	
	/**
	 * 删除
	 */
	@RequestMapping(value="/delete")
	public void delete(PrintWriter out){
		logBefore(logger, "删除RegisteronLineTran");
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "del")){return;} //校验权限
		PageData pd = new PageData();
		try{
			pd = this.getPageData();
			PageData registeron = registeronlinetranService.findById(pd);
			Session session = getSession();
			User user = (User) session.getAttribute(Const.SESSION_USER);
			if(user == null){
				throw new Exception("请重新登录");
			}
			else{
				registeron.put("RL_TYPE",2);
				registeron.put("RL_STATE",3);
				registeron.put("RL_USERNAME",user.getUSER_ID());
				registeron.put("RL_FINAL_TIME",DateUtil.getTime());
				registeronlinetranService.edit(registeron);
			}
			out.write("success");
			out.close();
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		
	}
	
	/**
	 * 修改
	 */
	@RequestMapping(value="/edit")
	public ModelAndView edit() throws Exception{
		logBefore(logger, "修改RegisteronLineTran");
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "edit")){return null;} //校验权限
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		registeronlinetranService.edit(pd);
		mv.addObject("msg","success");
		mv.setViewName("save_result");
		return mv;
	}
	
	/**
	 * 列表
	 */
	@RequestMapping(value="/list")
	public ModelAndView list(Page page){
		logBefore(logger, "列表RegisteronLineTran");
		//if(!Jurisdiction.buttonJurisdiction(menuUrl, "cha")){return null;} //校验权限
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		try{
			Session session = getSession();
			User user = (User) session.getAttribute(Const.SESSION_USER);
			if(user == null){
				throw new Exception("请重新登录");
			}
			pd = this.getPageData();
			pd.put("AIRLINERUMMERY_ID",user.getAIRLINERUMMERY_ID());
			pd.put("AIRLINEINFO_ID",user.getAIRLINEINFO_ID());
			page.setPd(pd);
			List<PageData>	varList = registeronlinetranService.list(page);	//列出RegisteronLineTran列表
			DecimalFormat format = new DecimalFormat("######0.00");
			for (PageData data:varList){
				data.put("RL_TYPE_VALUE", EnFinalValue.getName("BLQR",(Integer) data.get("RL_TYPE")));
				data.put("RL_STATE_VALUE", EnFinalValue.getName("BLZT",(Integer) data.get("RL_STATE")));
				Double amt = Double.valueOf((Integer)data.get("RL_AMOUNT"))/100;
				data.put("RL_AMOUNT",format.format(amt));
			}
			mv.setViewName("airsys/registeronlinetran/registeronlinetran_list");
			mv.addObject("varList", varList);
			mv.addObject("pd", pd);
			mv.addObject(Const.SESSION_QX,this.getHC());	//按钮权限
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		return mv;
	}

	/**
	 * 列表
	 */
	@RequestMapping(value="/managerList")
	public ModelAndView managerList(Page page){
		logBefore(logger, "列表managerList");
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		try{
			Session session = getSession();
			User user = (User) session.getAttribute(Const.SESSION_USER);
			if(user == null){
				throw new Exception("请重新登录");
			}
			pd = this.getPageData();
			pd.put("AIRLINERUMMERY_ID",user.getAIRLINERUMMERY_ID());
			pd.put("AIRLINEINFO_ID",user.getAIRLINEINFO_ID());
			page.setPd(pd);
			List<PageData>	varList = registeronlinetranService.list(page);	//列出RegisteronLineTran列表
			DecimalFormat format = new DecimalFormat("######0.00");
			for (PageData data:varList){
				data.put("RL_TYPE_VALUE", EnFinalValue.getName("BLQR",(Integer) data.get("RL_TYPE")));
				data.put("RL_STATE_VALUE", EnFinalValue.getName("BLZT",(Integer) data.get("RL_STATE")));
				Double amt = Double.valueOf((Integer)data.get("RL_AMOUNT"))/100;
				data.put("RL_AMOUNT",format.format(amt));
			}
			mv.setViewName("airsys/registeronlinetran/registeronlinetran_manager_list");
			mv.addObject("varList", varList);
			mv.addObject("pd", pd);
			mv.addObject(Const.SESSION_QX,this.getHC());	//按钮权限
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		return mv;
	}
	
	/**
	 * 去新增页面
	 */
	@RequestMapping(value="/goAdd")
	public ModelAndView goAdd(){
		logBefore(logger, "去新增RegisteronLineTran页面");
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try {
			Session session = getSession();
			User user = (User) session.getAttribute(Const.SESSION_USER);
			if(user == null){
				throw new Exception("请重新登录");
			}
			else{
				pd.put("AIRLINEINFO_ID",user.getAIRLINEINFO_ID());
			}
			List<PageData> empList = airlineemployeeService.listAll(pd);
			mv.addObject("empList",empList);
			mv.addObject("cmpName", user.getAL_NAME());
			mv.addObject("riName", user.getRI_NAME());
			mv.setViewName("airsys/registeronlinetran/registeronlinetran_edit");
			mv.addObject("msg", "save");
			mv.addObject("pd", pd);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		}						
		return mv;
	}	
	
	/**
	 * 去修改页面
	 */
	@RequestMapping(value="/goEdit")
	public ModelAndView goEdit(){
		logBefore(logger, "去修改RegisteronLineTran页面");
		ModelAndView mv = this.getModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try {
			Session session = getSession();
			User user = (User) session.getAttribute(Const.SESSION_USER);
			if(user == null){
				throw new Exception("请重新登录");
			}
			else{
				pd.put("AIRLINEINFO_ID",user.getAIRLINEINFO_ID());
			}
			List<PageData> empList = airlineemployeeService.listAll(pd);
			mv.addObject("empList",empList);
			mv.addObject("cmpName", user.getAL_NAME());
			mv.addObject("riName", user.getRI_NAME());
			pd = registeronlinetranService.findById(pd);	//根据ID读取
			pd.put("RL_TIME", DateUtil.getYYYY_MM_DD_HH_MM_SS(pd.getString("RL_TIME")));
			pd.put("TD_TIME", DateUtil.getYYYY_MM_DD_HH_MM_SS(pd.getString("TD_TIME")));
			DecimalFormat format = new DecimalFormat("######0.00");
			Double amt = Double.valueOf((Integer)pd.get("RL_AMOUNT"))/100;
			pd.put("RL_AMOUNT",format.format(amt));
			mv.setViewName("airsys/registeronlinetran/registeronlinetran_edit");
			mv.addObject("msg", "edit");
			mv.addObject("pd", pd);
			mv.addObject("pdObj",JsonUtil.toJson(pd));

		} catch (Exception e) {
			logger.error(e.toString(), e);
		}						
		return mv;
	}	
	
	/**
	 * 批量删除
	 */
	@RequestMapping(value="/deleteAll")
	@ResponseBody
	public Object deleteAll() {
		logBefore(logger, "批量删除RegisteronLineTran");
		if (!Jurisdiction.buttonJurisdiction(menuUrl, "dell")) {
			return null;
		} //校验权限
		PageData pd = new PageData();
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			pd = this.getPageData();
			List<PageData> pdList = new ArrayList<PageData>();
			String DATA_IDS = pd.getString("DATA_IDS");
			if (null != DATA_IDS && !"".equals(DATA_IDS)) {
				String ArrayDATA_IDS[] = DATA_IDS.split(",");
				registeronlinetranService.deleteAll(ArrayDATA_IDS);
				pd.put("msg", "ok");
			} else {
				pd.put("msg", "no");
			}
			pdList.add(pd);
			map.put("list", pdList);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		} finally {
			logAfter(logger);
		}
		return AppUtil.returnObject(pd, map);
	}

	/**
	 * 批量处理取消补录操作
	 */
	@RequestMapping(value="/cancelTran")
	@ResponseBody
	public Object cancelTran() {
		logBefore(logger, "批量删除RegisteronLineTran");
		if (!Jurisdiction.buttonJurisdiction("registeronlinetran/managerList", "dell")) {
			return null;
		}
		PageData pd = new PageData();
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			pd = this.getPageData();
			Session session = getSession();
			User user = (User) session.getAttribute(Const.SESSION_USER);
			if(user == null){
				throw new Exception("请重新登录");
			}
			List<PageData> pdList = new ArrayList<PageData>();
			String DATA_IDS = pd.getString("DATA_IDS");
			if (null != DATA_IDS && !"".equals(DATA_IDS)) {
				String ArrayDATA_IDS[] = DATA_IDS.split(",");
				registeronlinetranService.cancelRegisterTran(ArrayDATA_IDS,user);
				pd.put("msg", "ok");
			} else {
				pd.put("msg", "no");
			}
			pdList.add(pd);
			map.put("list", pdList);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		} finally {
			logAfter(logger);
		}
		return AppUtil.returnObject(pd, map);
	}

	/**
	 * 批量处理取消补录操作
	 */
	@RequestMapping(value="/commitTran")
	@ResponseBody
	public Object commitTran() {
		logBefore(logger, "批量处理取消补录操作");
		if (!Jurisdiction.buttonJurisdiction("registeronlinetran/managerList", "dell")) {
			return null;
		}
		PageData pd = new PageData();
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			pd = this.getPageData();
			Session session = getSession();
			User user = (User) session.getAttribute(Const.SESSION_USER);
			if(user == null){
				throw new Exception("请重新登录");
			}
			List<PageData> pdList = new ArrayList<PageData>();
			String DATA_IDS = pd.getString("DATA_IDS");

			if (null != DATA_IDS && !"".equals(DATA_IDS)) {
				String ArrayDATA_IDS[] = DATA_IDS.split(",");
				Boolean check = registeronlinetranService.checkRegisterTran(ArrayDATA_IDS);
				if(check){
					registeronlinetranService.commitRegisterTran(ArrayDATA_IDS,user);
					pd.put("msg", "ok");
				}
				else
				{
					pd.put("msg", "check");
				}

			} else {
				pd.put("msg", "no");
			}
			pdList.add(pd);
			map.put("list", pdList);
		} catch (Exception e) {
			logger.error(e.toString(), e);
		} finally {
			logAfter(logger);
		}
		return AppUtil.returnObject(pd, map);
	}
	
	/*
	 * 导出到excel
	 * @return
	 */
	@RequestMapping(value="/excel")
	public ModelAndView exportExcel(){
		logBefore(logger, "导出RegisteronLineTran到excel");
		if(!Jurisdiction.buttonJurisdiction(menuUrl, "cha")){return null;}
		ModelAndView mv = new ModelAndView();
		PageData pd = new PageData();
		pd = this.getPageData();
		try{
			Map<String,Object> dataMap = new HashMap<String,Object>();
			List<String> titles = new ArrayList<String>();
			titles.add("补录流水");	//1
			titles.add("公司编码");	//2
			titles.add("系统内员工编码");	//3
			titles.add("系统信用账户号");	//4
			titles.add("酒店编码");	//5
			titles.add("交易金额");	//6
			titles.add("交易日期");	//7
			titles.add("登记时间");	//8
			titles.add("确认时间");	//9
			titles.add("确认类型");	//10
			titles.add("确认人");	//11
			titles.add("补录状态");	//12
			dataMap.put("titles", titles);
			List<PageData> varOList = registeronlinetranService.listAll(pd);
			List<PageData> varList = new ArrayList<PageData>();
			for(int i=0;i<varOList.size();i++){
				PageData vpd = new PageData();
				vpd.put("var1", varOList.get(i).getString("RL_CODE"));	//1
				vpd.put("var2", varOList.get(i).getString("AL_CODE"));	//2
				vpd.put("var3", varOList.get(i).getString("EI_CODE"));	//3
				vpd.put("var4", varOList.get(i).getString("SA_ACC"));	//4
				vpd.put("var5", varOList.get(i).getString("RI_CODE"));	//5
				vpd.put("var6", varOList.get(i).getString("RL_AMOUNT"));	//6
				vpd.put("var7", varOList.get(i).getString("TD_TIME"));	//7
				vpd.put("var8", varOList.get(i).getString("RL_TIME"));	//8
				vpd.put("var9", varOList.get(i).getString("RL_FINAL_TIME"));	//9
				vpd.put("var10", varOList.get(i).getString("RL_TYPE"));	//10
				vpd.put("var11", varOList.get(i).getString("RL_USERNAME"));	//11
				vpd.put("var12", varOList.get(i).getString("RL_STATE"));	//12
				varList.add(vpd);
			}
			dataMap.put("varList", varList);
			ObjectExcelView erv = new ObjectExcelView();
			mv = new ModelAndView(erv,dataMap);
		} catch(Exception e){
			logger.error(e.toString(), e);
		}
		return mv;
	}
	
	/* ===============================权限================================== */
	public Map<String, String> getHC(){
		Subject currentUser = SecurityUtils.getSubject();  //shiro管理的session
		Session session = currentUser.getSession();
		return (Map<String, String>)session.getAttribute(Const.SESSION_QX);
	}
	/* ===============================权限================================== */

	public void initBinder(WebDataBinder binder){
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		binder.registerCustomEditor(Date.class, new CustomDateEditor(format,true));
	}
}
